package br.ufpr.tads.Rent.model;

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author vheofacker
 */
import br.ufpr.tads.Rent.controller.Automovel;
import br.ufpr.tads.Rent.controller.EnumClass.categoria;
import br.ufpr.tads.Rent.controller.EnumClass.marca;
import br.ufpr.tads.Rent.controller.Motocicleta;
import br.ufpr.tads.Rent.controller.Van;
import br.ufpr.tads.Rent.model.DevolveDTO;
import static com.sun.org.apache.xalan.internal.lib.ExsltDatetime.date;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;


public class DevolveDAO {
    private Connection cn;
    private static final String sqlselectdevolucao = "select a.nome,a.sobrenome,b.placa,b.marca,b.ano,c.dtloc,b.categoria,c.numdiasloc,b.modelo,c.idLocacao,b.idVeiculo,b.tipo from cliente a, veiculo b, locacao c where b.estado=1 and(a.idCliente=c.idCliente and b.idVeiculo=c.idVeiculo)";
    
    
    private static final String sqlatualizalocacao ="delete from locacao where idlocacao=?";
    private static final String sqlatualizaveiculo ="update veiculo set situacao=NULL where id=?";
    private static final String sqlgetid ="select idVeiculo from locacao where idlocacao = )";
    public DevolveDAO() throws FileNotFoundException, ClassNotFoundException, IOException, SQLException {
        this.cn =  new ConnectionFactory().getConnection();
    }
    public List<DevolveDTO> SelecionaDevolucao ()throws SQLException, IOException, ParseException{
        PreparedStatement stmt = cn.prepareStatement(sqlselectdevolucao);
        List<DevolveDTO> lista = new ArrayList ();
        ResultSet rs = stmt.executeQuery();
        while(rs.next()){ 
        DevolveDTO ct = new DevolveDTO();
        ct.setPlaca(rs.getString("placa")); 
        ct.setModelo(rs.getInt("modelo"));
        ct.setMarca(marca.values()[rs.getInt("marca")]);
        int cat = rs.getInt("categoria"); 
        ct.setNome(rs.getString("nome"));
        ct.setSobrenome(rs.getString("sobrenome"));
        int numdias = rs.getInt("numDiasLoc");
        ct.setDias(numdias);
        String dt = rs.getString("dtLoc");
        SimpleDateFormat dtp = new SimpleDateFormat("yyyy-dd-mm"); 
        Calendar date = Calendar.getInstance();
        date.setTime(dtp.parse(dt));
        ct.setDataloca(date);
        ct.setIdLoc(rs.getInt("idLocacao"));
        ct.setIdVei(rs.getInt("idVeiculo"));
        ct.setAno(rs.getInt("ano"));
        ct.setTipo(rs.getInt("tipo"));
        Automovel aut; 
        Motocicleta moto = null;
        Van van = null;
            try {
                aut = new Automovel();
                double valordiaria;
                if(rs.getInt("tipo")==0)
                    valordiaria = aut.getValorDiariaLocacao(rs.getInt("idVeiculo"));
                else if(rs.getInt("tipo")==1)
                    valordiaria = moto.getValorDiariaLocacao(rs.getInt("idVeiculo"));
                else
                    valordiaria = van.getValorDiariaLocacao(rs.getInt("idVeiculo"));
                double valortotal = valordiaria * numdias;
                ct.setDiaria(valordiaria);
                ct.setTotal(valortotal); 
                lista.add(ct);
            } catch (FileNotFoundException ex) {
                Logger.getLogger(DevolveDAO.class.getName()).log(Level.SEVERE, null, ex);
            } catch (ClassNotFoundException ex) {
                Logger.getLogger(DevolveDAO.class.getName()).log(Level.SEVERE, null, ex);
            }
      
        } 
        rs.close();
        stmt.close();
        cn.close();
        return lista;
    }
    public void devolucao(int IDLoc)throws FileNotFoundException, ClassNotFoundException, IOException, SQLException {
        this.cn =  new ConnectionFactory().getConnection();{
        PreparedStatement stmt = cn.prepareStatement(sqlatualizalocacao);
        stmt.setInt(1, IDLoc);
        stmt.executeUpdate();
        stmt.close();  
        cn.close();
    
    }
    }
    public int getIDVei (int idloc){
        PreparedStatement stmt;
        ResultSet rs;
        int id = 0;
        try {
            stmt = cn.prepareStatement(sqlgetid);
            stmt.setInt(1, idloc);
            rs = stmt.executeQuery();
            while(rs.next()){
             id = rs.getInt("idVeiculo");
            }
            
            return id;
        } catch (SQLException ex) {
            Logger.getLogger(DevolveDAO.class.getName()).log(Level.SEVERE, null, ex);
        }
        return 0;
    }
}

    
